package com.meilian.carousel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meilian.carousel.mapper.CarouselMapper;
import com.meilian.pojo.Carousel;
import com.meilian.carousel.service.CarouselService;
import com.meilian.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;
    /**
     * 查询优先级最高的六条轮播图数据
     * 按照优先级查询数据库数据
     * 使用stream流,进行内存数据切割,保留6条数据
     * @return
     */
    @Override
    public R list() {
        QueryWrapper<Carousel> carouselWrapper=new QueryWrapper<>();
        carouselWrapper.orderByDesc("priority");
        List<Carousel> list=carouselMapper.selectList(carouselWrapper);
        //jdk1.8 stream
        List<Carousel> collect=list.stream().limit(6).collect(Collectors.toList());
        R ok=R.ok(collect);
        log.info( "CarouselServiceImpl.list 业务结束，结果:{}" , ok);

        return ok;
    }
}
